package com.qq.oauth;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.qq.util.ParseString;
import com.qq.util.QQRequest;
import com.qq.util.QParameter;
import com.qq.util.OAuthKey;;

public class Token {

	 /**
	 * @brief get a request token by appid and appkey
	 *        rfc1738 urlencode
	 * @param $appid
	 * @param $appkey
	 *
	 * @return a string, the format as follow: 
	 *      oauth_token=xxx&oauth_token_secret=xxx
	 */
	public String getRequestToken(String appid, String appkey) {
		String url = "http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token";
		List<QParameter> parameters = new ArrayList<QParameter>();
		OAuthKey oauthKey = new OAuthKey();
		oauthKey.customKey = appid;
		oauthKey.customSecrect = appkey;
		//The OAuth Call back URL(You should encode this url if it
		//contains some unreserved characters).
		QQRequest request = new QQRequest();
		String res = null;
		try {
			res = request.syncRequest(url, "GET", oauthKey, parameters, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	/**
	 * @brief get a access token 
	 *        rfc1738 urlencode
	 * @param $appid
	 * @param $appkey
	 * @param $request_token
	 * @param $request_token_secret
	 * @param $vericode
	 *
	 * @return a string, as follows:
	 *      oauth_token=xxx&oauth_token_secret=xxx&openid=xxx&oauth_signature=xxx&oauth_vericode=xxx&timestamp=xxx
	 */
	public String getAccessToken(String appid, String appkey, String oauth_token, String oauth_token_secret, String oauth_vericode) {

		String url = "http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token";
		List<QParameter> parameters = new ArrayList<QParameter>();
		OAuthKey oauthKey = new OAuthKey();
		oauthKey.customKey = appid;
		oauthKey.customSecrect = appkey;
		oauthKey.tokenKey = oauth_token;
		oauthKey.tokenSecrect = oauth_token_secret;
		QParameter oauth_vericodep = new QParameter("oauth_vericode",oauth_vericode);
		parameters.add(oauth_vericodep);

		QQRequest request = new QQRequest();
		String res = null;
		try {
			res = request.syncRequest(url, "GET", oauthKey, parameters, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
}
